/**
 * 转成字节数组
 */
export  function hexToArray(hexStr) {
	const words = []
	let hexStrLength = hexStr.length

	if (hexStrLength % 2 !== 0) {
		hexStr = leftPad(hexStr, hexStrLength + 1)
	}

	hexStrLength = hexStr.length

	for (let i = 0; i < hexStrLength; i += 2) {
		words.push(parseInt(hexStr.substr(i, 2), 16))
	}
	return words
}

/**
 * 补全16进制字符串
 */
function leftPad(input, num) {
  if (input.length >= num) return input

  return (new Array(num - input.length + 1)).join('0') + input
}

export  function arrayBufferToBase64(buffer) {
	let binary = "";
	const bytes = new Uint8Array(buffer);
	const len = bytes.byteLength;
	for (let i = 0; i < len; i++) {
		binary += String.fromCharCode(bytes[i]);
	}
	//window.btoa():将ascii字符串或二进制数据转换成一个base64编码过的字符串,该方法不能直接作用于Unicode字符串.
	return window.btoa(binary);
}